#define DIRECTINPUT_VERSION 0x0800

#include <dinput.h>
#include "SettingsInputComponent.h"
#include "EngineMessages.h"
#include "HardwareSystemMessages.h"
#include "Message.h"

BYTE SettingsInputComponent::bindings[2] = {DIK_F, DIK_ESCAPE};
IEngineComponentRegistry settingsInputComponentRegistery("SettingsInputComponent", SettingsInputComponent::Deserialize);

SettingsInputComponent::SettingsInputComponent()
{
}

SettingsInputComponent::~SettingsInputComponent(void)
{
}

void SettingsInputComponent::getUpBindings(BYTE** bindings, DWORD* nBindings)
{
	*bindings = this->bindings;
	*nBindings = 2;
}

void SettingsInputComponent::keyUp(BYTE key, LONGLONG timeElapsed)
{
	UNREFERENCED_PARAMETER(timeElapsed);

	Message m;

	switch (key)
	{
		case DIK_F:
			m.messageId = GRAPHICS_TOGGLE_FULLSCREEN_MESSAGE;
			m.uiParam = 0;
			messages->Enqueue(m);
			break;
		case DIK_ESCAPE:
			m.messageId = ENGINE_QUIT_MESSAGE;
			m.uiParam = 0;
			messages->Enqueue(m);
			break;
	}
}

void SettingsInputComponent::Serialize(EnginePointer<ISerializerWriteContext>& context, char* name)
{
	context->StartItem(name, "SettingsInputComponent", "1.0");
	context->SerializeProperty("componentId", this->componentId);
	context->EndItem(name);
}

IEngineComponent* SettingsInputComponent::Deserialize(EnginePointer<ISerializerReadContext>& context)
{
	SettingsInputComponent* input = new SettingsInputComponent();
	int index = 0;
	char* childName = context->ChildName(index);

	while (childName != NULL)
	{
		if (strcmp(childName, "componentId") == 0)
		{
			input->componentId = context->ChildValueAsULongLong(index);
		}
		childName = context->ChildName(++index);
	}
	
	return input;
}